package slw.border
{
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	/**
	 * TitleBorder
	 * 标题边框类
	 * <p>
	 * 	<listing>
	 * 		var b:TitleBorder=new TitleBorder("Title","bottom");
			this.addChild(b);
	 * 	</listing>
	 * </p>
	 * @author ddx<br/>
	 * 2011-7-15
	 *
	 */
	public class TitleBorder extends Decorator
	{
		//
		/**
		 * "top"标题位于顶部 
		 */		
		public static const TOP:String="top";
		/**
		 * "bottom"标题位于底部  
		 */		
		public static const BOTTOM:String="bottom";
		/**
		 * "left"标题位于左边 
		 */		
		public static const LEFT:String="left";
		/**
		 * "right"标题位于右边 
		 */		
		public static const RIGHT:String="right";
		/**
		 * "center"标题位于中间 
		 */		
		public static const CENTER:String="center";
		
		//颜色，标题，位置，圆角，线宽，文本与线条距离
		private var _color:uint;
		private var _title:String;
		private var _position:String;
		private var _round:Number;
		private var _thickness:Number;
		private var _gap:Number=1;
		
		//宽，高
		private var _width:Number=0;
		private var _height:Number=0;
		
		//标题文本
		private var _titleText:TextField;
		//标题文本格式
		private var _format:TextFormat;
		
		/**
		 * 构造函数 
		 * @param title:String="" 标题
		 * @param position:String="top" 标题位置
 		 * @param thickness:Number=0.1 线宽
		 * @param color=0x999999 线颜色
		 * @param round:Number=5 圆角大小
		 * 
		 */		
		public function TitleBorder(title:String="",position:String=TOP,thickness:Number=0.1,color:uint=0x999999,round:Number=5)
		{
			super();
			
			this.listName="TitleBorder";
			
			init();
			
			setProperties(title,position,thickness,color,round,100,100);
		}
		
		/*
		-----------------------------------
		setters getters
		-----------------------------------
		*/
		//
		/**
		 * 标题占用边界的大小，当标题位于中间时，所占宽度为边框的宽度而非标题所占宽度 
		 * @return Number
		 * 
		 */		
		public function get titleSize():Number{
			switch(_position){
				case TOP:
					return _titleText.height;
					break;
				case BOTTOM:
					return _titleText.height;
					break;
				case CENTER:
					return _thickness;
					break;
				case LEFT:
					return _titleText.width;
					break;
				case RIGHT:
					return _titleText.width;
					break;
			}
			
			return 0;
		}
		//
		/**
		 * 标题文本默认格式 
		 * @param format:TextFormat
		 * 
		 */		
		public function set defaultTextFormat(format:TextFormat):void{
			if(_titleText.defaultTextFormat==format){
				return;
			}
			
			format.align=TextFormatAlign.CENTER;
			_titleText.defaultTextFormat=format;
			_format=null;
			
			updateDisplayList();
		}
		/**
		 * 标题文本默认格式 
		 * @param format:TextFormat
		 * 
		 */		
		public function get defaultTextFormat():TextFormat{
			return _titleText.defaultTextFormat;
		}
		//
		/**
		 * 标题文本颜色 
		 * @param value:uint 默认为0x0
		 * 
		 */		
		public function set textColor(value:uint):void{
			if(_titleText.textColor==value){
				return;
			}
			_titleText.textColor=value;
		}
		//
		/**
		 * 标题文本颜色 
		 * @param value:uint 默认为0x0
		 * 
		 */
		public function get textColor():uint{
			return _titleText.textColor;
		}
		//
		/**
		 * 标题 
		 * @param value:String
		 * 
		 */		
		public function set title(value:String):void{
			if(_title==value){
				return;
			}
			_title=value;
			
			updateDisplayList();
		}
		//
		/**
		 * 标题 
		 * @param value:String
		 * 
		 */		
		public function get title():String{
			return _title;
		}
		//
		/**
		 * 标题位置
		 * @param value:String 默认值:TitleBorder.TOP,可能的值:<br/>
		 * <ul>
		 * <li>TitleBorder.TOP 标题位于边框顶部<br/>
		 * <li>TitleBorder.BOTTOM 标题位于边框底部<br/>
		 * <li>TitleBorder.CENTER 标题位于边框中间<br/>
		 * <li>TitleBorder.LEFT 标题位于边框左边<br/>
		 * <li>TitleBorder.RIGHT 标题位于边框右边<br/>
		 * </ul>
		 * 
		 */		
		public function set position(value:String):void{
			if(_position==value){
				return;
			}
			_position=value;
			
			updateDisplayList();
		}
		//
		/**
		 * 标题位置
		 * @param value:String 默认值:TitleBorder.TOP,可能的值:<br/>
		 * <ul>
		 * <li>TitleBorder.TOP 标题位于边框顶部<br/>
		 * <li>TitleBorder.BOTTOM 标题位于边框底部<br/>
		 * <li>TitleBorder.CENTER 标题位于边框中间<br/>
		 * <li>TitleBorder.LEFT 标题位于边框左边<br/>
		 * <li>TitleBorder.RIGHT 标题位于边框右边<br/>
		 * </ul>
		 * 
		 */
		public function get position():String{
			return _position;
		}
		//
		/**
		 * 边框宽度  
		 * @param value:Number
		 * 
		 */		
		public function set thickness(value:Number):void{
			if(_thickness==value){
				return;
			}
			_thickness=value;
			
			updateDisplayList();
		}
		//
		/**
		 * 边框宽度  
		 * @param value:Number
		 * 
		 */		
		public function get thickness():Number{
			return _thickness;
		}
		//
		/**
		 * 边框颜色 
		 * @param value:uint 默认值:0x999999
		 * 
		 */		
		public function set color(value:uint):void{
			if(_color==value){
				return;
			}
			_color=value;
			
			updateDisplayList();
		}
		//
		/**
		 * 边框颜色 
		 * @param value:uint 默认值:0x999999
		 * 
		 */
		public function get color():uint{
			return _color;
		}
		//
		/**
		 * 圆角大小 
		 * @param value:Number 默认值:5
		 * 
		 */		
		public function set round(value:Number):void{
			if(_round==value){
				return;
			}
			_round=value;
			
			updateDisplayList();
		}
		//
		/**
		 * 圆角大小 
		 * @param value:Number 默认值:5
		 * 
		 */
		public function get round():Number{
			return _round;
		}
		//
		override public function set width(value:Number):void{
			if(super.width==value){
				return;
			}
			drawing(this,value,_height,0,0,0);
			_width=value;
			
			updateDisplayList();
		}
		//
		override public function get width():Number{
			return _width==0?super.width:_width;
		}
		//
		override public function set height(value:Number):void{
			if(super.height==value){
				return;
			}
			drawing(this,_width,value,0,0,0);
			_height=value;
			
			updateDisplayList();
		}
		//
		override public function get height():Number{
			return _height==0?super.height:_height;
		}
		/*
		-----------------------------------
		public  methods
		-----------------------------------
		*/
		//
		/**
		 * 设置标题文本格式 
		 * @param format:TextFormat
		 * 
		 */		
		public function setTextFormat(format:TextFormat):void{
			_format=format;
			_format.align=TextFormatAlign.CENTER;
			
			updateDisplayList();
		}
		/*
		-----------------------------------
		private  methods
		-----------------------------------
		*/
		//初始化
		private function init():void{
			this.mouseChildren=false;
			
			_titleText=new TextField();
			setMultiline(_titleText,false);
			_titleText.autoSize=TextFieldAutoSize.LEFT;
			
			var $format:TextFormat=_titleText.defaultTextFormat;
			$format.align=TextFormatAlign.CENTER;
			_titleText.defaultTextFormat=$format;
			this.addChild(_titleText);
		}
		//设置属性
		private function setProperties(title:String,position:String,thickness:Number,color:uint,round:Number,width:Number,height:Number):void{
			_title=title;
			_position=position;
			_thickness=thickness;
			_color=color;
			_round=round;
			
			_width=width;
			_height=height;
			
			updateDisplayList();
		}
		//设置文本多行
		private function setMultiline(textField:TextField,multiline:Boolean):void{
			textField.multiline=false;
			textField.wordWrap=false;
		}
		//更新显示
		private function updateDisplayList():void{
			var $title:String=_title;
			switch(_position){
				case TOP:
					setMultiline(_titleText,false);
					$title=clearSign(_title,"\n");
					break;
				case BOTTOM:
					setMultiline(_titleText,false);
					$title=clearSign(_title,"\n");
					break;
				case CENTER:
					setMultiline(_titleText,false);
					$title=clearSign(_title,"\n");
					break;
				case LEFT:
					setMultiline(_titleText,true);
					_titleText.width=getWidth(_title);
					
					$title=addSign(_title,"\n");
					break;
				case RIGHT:
					setMultiline(_titleText,true);
					_titleText.width=getWidth(_title);
					$title=addSign(_title,"\n");
					break;
			}
			_titleText.text=$title;
			if(_format!=null)
				_titleText.setTextFormat(_format);
			
			if(_width<=_titleText.width||_height<=_titleText.height){
				return;
			}
			
			this.graphics.lineStyle(_thickness,_color,1,true,"none","round");
			switch(_position){
				case TOP:
					_titleText.x=(_width-_titleText.width)/2;
					_titleText.y=0;
					
					moveTo(0,_titleText.height/2+_round);
					curveTo(0,_titleText.height/2,_round,_titleText.height/2);
					
					this.graphics.lineStyle(_thickness,_color,1,true,"none","none");
					lineTo(_titleText.x-_gap,_titleText.height/2);//top
					moveTo(_titleText.x+_titleText.width+_gap,_titleText.height/2);
					lineTo(_width-_round,_titleText.height/2);
					
					this.graphics.lineStyle(_thickness,_color,1,true,"none","round");
					curveTo(_width,_titleText.height/2,_width,_titleText.height/2+_round);
					
					lineTo(_width,_height-_round);//right
					
					curveTo(_width,_height,_width-_round,_height);
					
					lineTo(_round,_height);//bottom
					
					curveTo(0,_height,0,_height-_round);
					
					lineTo(0,_titleText.height/2+_round);//left
					break;
				case BOTTOM:
					_titleText.x=(_width-_titleText.width)/2;
					_titleText.y=_height-_titleText.height;
					
					moveTo(0,_round);
					curveTo(0,0,_round,0);
					
					lineTo(_width-_round,0);//top
					
					curveTo(_width,0,_width,_round);
					
					lineTo(_width,_height-_titleText.height/2-_round);//right
					
					curveTo(_width,_height-_titleText.height/2,_width-_round,_height-_titleText.height/2);
					
					this.graphics.lineStyle(_thickness,_color,1,true,"none","none");
					lineTo(_titleText.x+_titleText.width+_gap,_height-_titleText.height/2);//bottom
					moveTo(_titleText.x-_gap,_height-_titleText.height/2);
					lineTo(_round,_height-_titleText.height/2);
					
					this.graphics.lineStyle(_thickness,_color,1,true,"none","round");
					curveTo(0,_height-_titleText.height/2,0,_height-_titleText.height/2-_round);
					
					lineTo(0,_round);//left
					break;
				case CENTER:
					_titleText.x=(_width-_titleText.width)/2;
					_titleText.y=(_height-_titleText.height)/2;
					
					moveTo(0,_round);
					curveTo(0,0,_round,0);
					
					lineTo(_width-_round,0);//top
					
					curveTo(_width,0,_width,_round);
					
					lineTo(_width,_height-_round);//right
					
					curveTo(_width,_height,_width-_round,_height);
					
					lineTo(_round,_height);//bottom
					
					curveTo(0,_height,0,_height-_round);
					
					lineTo(0,_round);//left
					break;
				case LEFT:
					_titleText.x=0;
					_titleText.y=(_height-_titleText.height)/2;
					
					moveTo(_titleText.width/2,_round);
					curveTo(_titleText.width/2,0,_titleText.width/2+_round,0);
					
					lineTo(_width-_round,0);
					curveTo(_width,0,_width,_round);
					
					lineTo(_width,_height-_round);
					curveTo(_width,_height,_width-_round,_height);
					
					lineTo(_titleText.width/2+_round,_height);
					curveTo(_titleText.width/2,_height,_titleText.width/2,_height-_round);
					
					this.graphics.lineStyle(_thickness,_color,1,true,"none","none");
					lineTo(_titleText.width/2,_titleText.y+_titleText.height+_gap);
					moveTo(_titleText.width/2,_titleText.y-_gap);
					
					lineTo(_titleText.width/2,_round);
					break;
				case RIGHT:
					_titleText.x=_width-_titleText.width;
					_titleText.y=(_height-_titleText.height)/2;
					
					moveTo(0,_round);
					curveTo(0,0,_round,0);
					
					lineTo(_width-_titleText.width/2-_round,0);
					curveTo(_width-_titleText.width/2,0,_width-_titleText.width/2,_round);
					
					this.graphics.lineStyle(_thickness,_color,1,true,"none","none");
					lineTo(_width-_titleText.width/2,_titleText.y-_gap);
					moveTo(_width-_titleText.width/2,_titleText.y+_titleText.height+_gap);
					lineTo(_width-_titleText.width/2,_height-_round);
					
					this.graphics.lineStyle(_thickness,_color,1,true,"none","round");
					curveTo(_width-_titleText.width/2,_height,_width-_titleText.width/2-_round,_height);
					
					lineTo(_round,_height);//bottom
					
					curveTo(0,_height,0,_height-_round);
					
					lineTo(0,_round);//left
					break;
			}
		}
		//
		private function moveTo(x:Number,y:Number):void{
			this.graphics.moveTo(x,y);
		}
		//
		private function lineTo(x:Number,y:Number):void{
			this.graphics.lineTo(x,y);
		}
		//
		private function curveTo(controlX:Number,controlY:Number,anchorX:Number,anchorY:Number):void{
			this.graphics.curveTo(controlX,controlY,anchorX,anchorY);
		}
		//当文本为竖直排列时的文本宽度
		private function getWidth(text:String):Number{
			var $width:Number=0;
			var $t:TextField=new TextField();
			$t.autoSize=TextFieldAutoSize.LEFT;
			for(var i:int=0;i<text.length;i++){
				$t.text=text.charAt(i);
				$width=Math.max($width,$t.width);
			}
			
			return $width;
		}
		//添加\n
		private function addSign(source:String,sign:String):String{
			var $temp:Array=source.split("");
			source=$temp.join(sign);
			
			return source;
		}
		//删除\n
		private function clearSign(source:String,sign:String):String{
			return source.replace(sign,"");
		}
		//改变组件宽高
		private function drawing(target:Sprite,width:Number,height:Number,thickness:Number=0,color:uint=0,alpha:Number=0):void{
			target.graphics.clear();
			target.graphics.lineStyle(thickness,color,alpha);
			target.graphics.drawRect(0,0,width,height);
		}
	}
}